﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quexcel.OpenXML.Interfaces;
using Quexcel.OpenXML.Excel.Factory;
using Quexcel.OpenXML.Transaction;
using System.IO.Packaging;
using System.Text.RegularExpressions;
using System.Xml;

namespace Quexcel.OpenXML.Excel.Style
{
    internal class StyleLoader
    {
        private IStyleReader styleReader;
        private static IStyleWriter styleWriter;

        private PackagePart DocumentPart { get; set; }

        private ExcelTransaction transaction;
        private Quexcel.OpenXML.Excel.Workbook.Workbook workbook;
        private string stylesheetName;

        internal StyleLoader(ExcelTransaction transaction, Quexcel.OpenXML.Excel.Workbook.Workbook workbook, PackagePart docPart)
        {
            this.DocumentPart = docPart;
            this.workbook = workbook;
            stylesheetName = (new Regex("(?<name>[^/]+).xml")).Match(docPart.Uri.ToString()).Result("${name}");
        }

        string Stylesheetame { get { return stylesheetName; } }
        Quexcel.OpenXML.Excel.Workbook.Workbook Workbook { get { return workbook; } }
        internal XmlNamespaceManager NameSpaceManager
        {
            get { return workbook.NameSpaceManager; }
        }

        #region cell style
        Dictionary<int, CellStyle> cellStyles;
        int cellStylesSize;
        public CellStyle CellStyles(int index)
        {
            if (cellStyles.ContainsKey(index))
                return cellStyles[index];
            
            return LoadCellStyle(index);
        }
        private CellStyle LoadCellStyle(int index)
        {
            if (CellStyles(index) == null)
            {
                CellStyle style = this.GetReader().LoadCellStyle(index);
                cellStyles.Add(index, style);
            }
            else
                return CellStyles(index);
            return null;
        }
        public int AddCellStyle(CellStyle style)
        {
            int index = this.GetWriter().AddCellStyle(style);
            if (index != -1)
            {
                cellStyles.Add(index, style);
            }

            return index;
        }

        private static StyleLoader styleLoader;

        public static StyleLoader GetInstance()
        {
            return styleLoader;
        }
        #endregion

        public IStyleReader GetReader()
        {
            if (styleReader == null)
                styleReader = ReaderWriterBuilder.GetStyleReader(DocumentPart, NameSpaceManager);
            return styleReader;
        }

        public IStyleWriter GetWriter()
        {
            if (styleWriter == null)
                styleWriter = ReaderWriterBuilder.GetStyleWriter(DocumentPart, NameSpaceManager);
            return styleWriter;
        }

        public bool HasWriter()
        {
            return styleReader != null;
        }

        public bool HasReader()
        {
            return styleWriter != null;
        }
    }
}
